package com.example.mvplibrary.mvplibrary.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

//Recyclerview.Adapter单布局适配器的抽取
public abstract class BaseAdapter<DATA> extends RecyclerView.Adapter<BaseViewHolder> {

    protected List<DATA> datas;
    protected Context context;
    protected LayoutInflater layoutInflater;
    private int layoutId;

    public BaseAdapter(List<DATA> datas, Context context, int layoutId) {
        this.datas = datas;
        this.context = context;
        layoutInflater=LayoutInflater.from(context);
        this.layoutId = layoutId;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BaseViewHolder(layoutInflater.inflate(layoutId,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        bindData(holder,datas.get(position),position);
    }

    protected abstract void bindData(BaseViewHolder holder, DATA data,int position);

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
}
